/**
 * 
 */
package bpm.dao.impl.document;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import bpm.domain.document.AttachmentType;

/**
 * @author ZhaoWangLong Aug 16, 2011
 *
 */
@Repository("attachmentTypeDao")
public class AttachmentTypeDaoImpl implements bpm.dao.document.AttachmentTypeDao {
	@Resource private JdbcTemplate jdbcTemplate;
	private static final org.apache.log4j.Logger logger=org.apache.log4j.Logger.getLogger(AttachmentTypeDaoImpl.class);
	/* (non-Javadoc)
	 * @see bpm.dao.document.AttachmentTypeDao#deleteAttachmentType(java.lang.Integer[])
	 */
	public void deleteAttachmentType(Integer[] typeIds) {
		StringBuffer sql=new StringBuffer();
		sql.append("delete from BPM_DC_ATTACH_TYPE where id in ( ");
		for (int i = 0; i < typeIds.length; i++) {
			if(i!=typeIds.length-1){
				sql.append(" ? ,");
			}else{
				sql.append(" ?  ) ");
			}
		}
		logger.debug("DeleteSQL:"+sql.toString());
		this.jdbcTemplate.update(sql.toString(),typeIds);
	}

	/* (non-Javadoc)
	 * @see bpm.dao.document.AttachmentTypeDao#findAll()
	 */
	@SuppressWarnings("unchecked")
	public List<AttachmentType> findAll() {
		String sql="select  ID,KEY, NAME, RANK, ENABLE, CREATE_TIME, LAST_UPDATE_TIME,DESCRIPTION from BPM_DC_ATTACH_TYPE "
			+" order by RANK asc";
		return this.jdbcTemplate.query(sql, new AttachmentTypeRowMapper());
	}

	/* (non-Javadoc)
	 * @see bpm.dao.document.AttachmentTypeDao#findAttachmentTypeById(java.lang.Integer)
	 */
	public AttachmentType findAttachmentTypeById(Integer typeId) {
		String sql="select  ID,KEY, NAME, RANK, ENABLE, CREATE_TIME, LAST_UPDATE_TIME,DESCRIPTION from BPM_DC_ATTACH_TYPE "
			+" where ID =?";
		List<?> list= this.jdbcTemplate.query(sql, new Object[]{typeId},new AttachmentTypeRowMapper());
		if(list!=null&&list.size()>0){
			return (AttachmentType)list.get(0);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see bpm.dao.document.AttachmentTypeDao#insertAttachmentType(bpm.domain.document.AttachmentType)
	 */
	public void insertAttachmentType(AttachmentType attachmentType) {
		String sql="insert into BPM_DC_ATTACH_TYPE (ID, KEY,NAME, RANK, ENABLE, CREATE_TIME, LAST_UPDATE_TIME,DESCRIPTION) "
			+" values (?,?,?,?,?,?,?)";
		
		this.jdbcTemplate.update(sql,new Object[]{
				attachmentType.getId(),attachmentType.getName(),
				attachmentType.getKey()==null?null:attachmentType.getKey().name(),
				attachmentType.getRank(),attachmentType.isEnable()?"1":"0",
				new java.sql.Date(attachmentType.getCreateTime().getTime()),
				new java.sql.Date(attachmentType.getLastUpdateTime().getTime()),
				attachmentType.getDescription()
		});
	}

	/* (non-Javadoc)
	 * @see bpm.dao.document.AttachmentTypeDao#updateAttachmentType(bpm.domain.document.AttachmentType)
	 */
	public void updateAttachmentType(AttachmentType attachmentType) {
		String sql="update BPM_DC_ATTACH_TYPE set KEY=?, NAME=?,RANK=?,ENABLE=?,LAST_UPDATE_TIME=?,DESCRIPTION=? where "
			+" ID=? ";
		this.jdbcTemplate.update(sql, new Object[]{
				attachmentType.getKey()==null?null:attachmentType.getKey().name(),
				attachmentType.getName(),
				attachmentType.getRank(),attachmentType.isEnable()?"1":"0",
				new java.sql.Date(attachmentType.getLastUpdateTime().getTime()),
			    attachmentType.getDescription(),attachmentType.getId()
		});
	}

	public Integer findMaxId() {
		String sql="select max(ID) from BPM_DC_ATTACH_TYPE ";
		return this.jdbcTemplate.queryForInt(sql);
	}

	@SuppressWarnings("unchecked")
	public List<AttachmentType> findByCategory(Integer category) {
		StringBuffer  sql=new StringBuffer();
			sql.append("select  ID,KEY,REFERENCE, PAPERY, NAME, RANK, ENABLE, CREATE_TIME, LAST_UPDATE_TIME,DESCRIPTION from BPM_DC_ATTACH_TYPE ")
			.append(" where ID in ( select  ATTACHTYPE_ID  from BPM_DC_ATTACHTYPE_CATEGORY where CATEGORY_ID=?)")
			.append(" order by RANK asc");
		return this.jdbcTemplate.query(sql.toString(),new Object[]{category} ,new AttachmentTypeRowMapper(true));
	}

	@SuppressWarnings("unchecked")
	public List<AttachmentType> findAttachmentTypeByKey(String key) {
		StringBuffer  sql=new StringBuffer();
		sql.append("select  ID,KEY, NAME, RANK, ENABLE, CREATE_TIME, LAST_UPDATE_TIME,DESCRIPTION from BPM_DC_ATTACH_TYPE ")
		.append(" where KEY=?")
		.append(" order by RANK asc");
		return this.jdbcTemplate.query(sql.toString(),new Object[]{key} ,new AttachmentTypeRowMapper());
	}

}
